<?php 
namespace Home\Controller;

use Think\Controller;

class ShopcartController extends Controller
{
	public function index()
	{
		// var_dump($_POST);die;
		if(empty($_POST) && empty($_GET)){
			$this->display();
			die;
		}
		if(empty($_POST)){
			$gid = $_GET['gid'];
			$num = 1;
		}else{
			$gid = $_POST['gid'];
			$num = I('post.num/d');
		}
		// var_dump($gid);
		
		// var_dump($num);die;
		$car = M('goods');
		$list = $car->field('img,gname,price,id,des,stock')->where('id ='.$gid)->select();
		$list[0]['num']=$num;
		// var_dump($list);die;
		$_SESSION['Mycar'][$gid]=$list[0];
		// var_dump($_SESSION);
		// 将商品的id转换成gid   T_T
		$y = [];
		foreach ($_SESSION['Mycar'] as $k => $v) {
		$y[$k] = $k;
		unset($_SESSION['Mycar'][$k]['id']);
		$_SESSION['Mycar'][$k]['gid']=$y[$k];
		}
		// var_dump($_SESSION);die;
		// var_dump($list);die;
		
		$this->assign("list",$list);
		
		$this->display();
	}

	public function del()
	{
		//删除商品时清除商品的SESSION
		foreach ($_SESSION['Mycar'] as $k => $v) {
			if($k==$_GET['gid']){
				unset($_SESSION['Mycar'][$k]);
				
			}
		}
	}
}